package com.zahb.qadx.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.model.TeacherModel;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by： huwei.
 * Data： 2020-10-18.
 * Time： 22:54.
 * Desc：个人介绍
 */
public class PersonIntroduceFragment extends BaseFragmentExt {
    @BindView(R.id.cateName)
    TextView cate_name;
    @BindView(R.id.tv_recommended)
    TextView tvRecommended;
    @BindView(R.id.tv_qualification)
    LinearLayout tvQualification;
    private String relationId;
    List<String> cateName = new ArrayList<>();
    List<String> certificates = new ArrayList<>();
    @Override
    protected int getLayout() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initViews(View rootView) {
        mUnbinder = ButterKnife.bind(this,rootView);
        tvRecommended.setText(getArguments().getString("cateName"));
//        tvQualification.setText(getArguments().getString("certificates"));
        tvRecommended.setText(getArguments().getString("desc"));
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .getTeacher(relationId, 1)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        getDetailData(response);
                    }, throwable -> {
                        hideProgressDialog();
                        throwable.printStackTrace();
                        Tips.RequestError(getActivity());
                    }));
        }
    }

    private void getDetailData(TeacherModel teacherModel) {
        if (teacherModel.getStatusCode() == 200) {
            cateName = teacherModel.getData().getCateName();
            certificates = teacherModel.getData().getCertificates();
            String s = cateName.toString();
            String name = s.replace("[", "").replace("]", "");
            cate_name.setText(!TextUtils.isEmpty(name) ? name : "暂无内容，请等待后期更新");
            tvRecommended.setText(!TextUtils.isEmpty(teacherModel.getData().getBriefIntroduce()) ? teacherModel.getData().getBriefIntroduce() : "暂无内容，请等待后期更新");
            for (int i = 0; i < certificates.size(); i++) {
                String s2 = certificates.get(i);
                ImageView imageView = new ImageView(getActivity());
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));  //设置图片宽高
                ImageLoaderKt.loadImageSameCorners(imageView,
                        CompatHelper.isEmpty(certificates.get(i)) ? "20200914/1600056121587689.jpg" : certificates.get(i), 5f);
                tvQualification.addView(imageView); //动态添加图片
            }
        } else {
            showBindToast(teacherModel.getErrorInfo());
        }
    }

    public void setParamse(String relationId) {
        this.relationId = relationId;
    }
}
